package day14;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
public class XMLRead {
    public static void main(String[] args) {
        //增删改读。。。
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            //XMLRead.class.getClassLoader().getResourceAsStream("day14/demo.xml");
            doc = reader.read(XMLRead.class.getClassLoader().getResourceAsStream("day14/demo.xml"));
            //doc = reader.read(new FileInputStream("java高级\\src\\day14\\demo.xml"));
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        Element root = doc.getRootElement();
        System.out.println("根节点的名称是"+root.getName());
        List<Element> list = root.selectNodes("book");
        for(Element e:list){
            int index = Integer.parseInt(e.attributeValue("index"));
            Element name = (Element)e.selectSingleNode("name");
            String bookname = name.getText();
            Element diff = (Element)e.selectSingleNode("difficult");
            String difficult = diff.getText();
            Element des = (Element)e.selectSingleNode("des");
            String descrp = des.getText();
            System.out.printf("序号=%d,名称=%s,难度 %s,描述=%s\n",index,bookname,difficult,descrp);
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

        }
    }
}
